<?php

namespace App\Admin\Controllers\Vote\Chapter;

use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Actions\Chapter\ChapterDomainConfig;
use App\Admin\Actions\Chapter\DownloadPoster;
use App\Admin\Actions\Chapter\ImportPlayer;
use App\Admin\Actions\Chapter\RebuildChapterPoster;
use App\Admin\Actions\Chapter\ReplicateChapter;
use App\Models\Chapter;
use App\Models\Season;
use App\Models\Wechat;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\Tools\ActionDivider;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;

class ChapterController extends AdminController
{
    protected string $title = '场次管理';

    public function grid(): Grid
    {
        $model = Chapter::ofAdmin()
            ->with(['season', 'wechat'])
            ->withCount(['players', 'sponsors'])
            ->orderByDesc('end_at')
            ->when(request()->season_id, function (Builder $query, $id) {
                $query->where('season_id', $id);
            });

        return Grid::make($model, function (Grid $grid) {
            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
                $grid->disableCreateButton();
                $grid->disableActions();
            }

            $grid->disableBatchDelete(false);
            $grid->batchActions([
                new BatchEnable(Chapter::class),
                new BatchDisable(Chapter::class),
            ]);

            $grid->actions([
                new RebuildChapterPoster,
                new ImportPlayer,
                new DownloadPoster,
                new ActionDivider,
                new ReplicateChapter,
                new ChapterDomainConfig,
            ]);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title', '活动标题');
                $filter->between('start_at', '开始时间')
                    ->datetime();
                $filter->between('end_at', '结束时间')
                    ->datetime();
            });
            $grid->quickSearch(['title'])
                ->placeholder('活动标题');

            $grid->column('season.title', '活动赛季');
            $grid->column('wechat.name', '微信公众号');
            $grid->column('title', '活动标题')
                ->link(fn() => route('admin.chapters.players.index', $this), '_self');
            $grid->column('players_count', '选手')
                ->link(fn() => route('admin.chapters.players.index', $this), '_self');
            # region 统计区
            $grid->column('浏览量')
                ->display(fn() => $this->players()->sum('clicks'));
            $grid->column('点赞')
                ->display(fn() => $this->likes_show)
                ->link(fn() => route('admin.chapters.likes.index', $this), '_self');
            $grid->column('评论')
                ->display(fn() => $this->comments_show)
                ->link(fn() => route('admin.chapters.comments.index', $this), '_self');
            $grid->column('支持')
                ->display(fn() => $this->supports_show)
                ->link(fn() => route('admin.chapters.supports.index', $this), '_self');
            # endregion
            $grid->column('sponsors_count', '赞助商')
                ->link(fn() => route('admin.chapters.sponsors.index', $this), '_self');
            $grid->column('status')
                ->bool();
            $grid->column('起始时间')->display(function () {
                if (Carbon::now()->between($this->start_at, $this->end_at)) {
                    $start = '<span style="color: green">'.$this->start_at.'</span><br>';
                    $end = '<span style="color: green">'.$this->end_at.'</span>';
                } else {
                    $start = '<span style="color: red">'.$this->start_at.'</span><br>';
                    $end = '<span style="color: red">'.$this->end_at.'</span>';
                }

                return $start.$end;
            });
            $grid->column('created_at');
        });
    }

    public function form(): Form
    {
        return Form::make(Chapter::ofAdmin(), function (Form $form) {
            $form->select('season_id', '赛季选择')
                ->width(4)
                ->required()
                ->options(Season::ofAdmin()->ofEnabled()->pluck('title', 'id'));
            $form->text('title', '活动标题')
                ->width(4)
                ->required();
            $form->datetimeRange('start_at', 'end_at', '起始时间')
                ->required();
            $form->select('wechat_id', '微信公众号')
                ->options(Wechat::ofAdmin()->ofEnabled()->pluck('name', 'id'))
                ->width(4)
                ->help('场次公众号配置，如果不配置，则使用赛季默认的公众号');
            $form->switch('status')
                ->default(true);
        });
    }
}
